Page({
  data: {
    newsItems: [
      { text: '消息：蜜雪冰城在2024年9月正式入住软件园校区食堂，欢迎同学们进行选购！！' },
      { text: '新消息：校园知行录正处于测试阶段。' }
    ],
    showDeleteBtn: false,
    touchStartX: 0,
    touchStartY: 0
  },

  onLoad(options) {},

  handleSwipeStart: function(e) {
    this.setData({ showDeleteBtn: false });
    this.setData({ touchStartX: e.touches[0].clientX });
    this.setData({ touchStartY: e.touches[0].clientY });
  },

  handleSwipeMove: function(e) {
    const touch = e.touches[0];
    const deltaX = touch.clientX - this.data.touchStartX;
    if (Math.abs(touch.clientY - this.data.touchStartY) > 10) return; // 忽略垂直方向的滑动

    if (deltaX < 0) {
      this.setData({ showDeleteBtn: true });
    } else {
      this.setData({ showDeleteBtn: false });
    }
  },

  handleSwipeEnd: function(e) {
    // 当手指离开屏幕时，设置删除按钮显示，并启动定时器
    this.setData({ showDeleteBtn: true });

    // 设置一个定时器，在5秒后隐藏删除按钮
    const hideAfterDelay = () => {
      this.setData({ showDeleteBtn: false });
    };

    // 启动定时器
    setTimeout(hideAfterDelay, 5000);
  },

  handleDelete: function(e) {
    const index = e.currentTarget.dataset.index;
    const items = this.data.newsItems;
    items.splice(index, 1);
    this.setData({ newsItems: items });
  }
});